/**
 * 
 */
package cn.ad.server.web.servlet.v2.requesthandler;


import cn.ad.server.web.servlet.v2.parser.AdRequestParseException;
import cn.ad.server.web.servlet.v2.parser.HttpServletParser;
import cn.ad.server.web.servlet.v2.parser.STSAdRequestParser;
import cn.ad.server.web.spring.model.IAdserverRequest;
import cn.ad.server.web.spring.model.IAdserverResponse;
import cn.ad.server.web.spring.model.sts.STSAdResponse;
import cn.ad.server.domain.enumration.ReturnCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;

/**
 * Server 2 Server 广告协议处理Handler
 * @author Wu Chengjun
 */
public class STSRequestHandler extends AdRequestHandler{
	
	private static Logger LOGGER = LoggerFactory.getLogger(STSRequestHandler.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * 
	 */
	public STSRequestHandler() {
		//设置协议解析器
		setParser(new STSAdRequestParser());
	}

	@Override
	public void handleExceptionResponse(Exception e, HttpServletResponse response) {
		if(e instanceof AdRequestParseException){
			STSAdResponse adResponse = new STSAdResponse();
			adResponse.setStatus(ReturnCode.E10008.getCode());
			try {
				getParser().writeResponse(adResponse, response);
			} catch (AdRequestParseException e1) {
				LOGGER.error("STS 写入响应结果出现异常, {}", e.getMessage(), e1);
			}
		}else{
			super.handleExceptionResponse(e, response);
		}
	}

	@Override
	public void handleResponse(HttpServletResponse response, HttpServletParser selvetParser, IAdserverRequest adrequest,
			IAdserverResponse adresponse) throws Exception {
		super.handleResponse(response, selvetParser, adrequest, adresponse);
		//LOGGER.info("CATCH RESPONSE: {}", JsonUtils.toJson(adresponse));
	}
}
